package org.nulldevice.tankshot.images;

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;

import javax.imageio.ImageIO;

public class ImageContainer {
	private static final String imagedir = "/org/nulldevice/tankshot/images/";
	/** The single instance of this class */
	private static ImageContainer single = new ImageContainer();

	/**
	 * Get the single instance of this class
	 * 
	 * @return The single instance of this class
	 */
	public static ImageContainer get() {
		return single;
	}

	/**
	 * The cached imagerepresentant map, from reference to imagerepresentant
	 * instance
	 */
	private HashMap imagerepresentants = new HashMap();

	/**
	 * Retrieve a imagerepresentant from the store
	 * 
	 * @param ref
	 *            The reference to the image to use for the imagerepresentant
	 * @return A imagerepresentant instance containing an accelerate image of
	 *         the request reference
	 */
	public ImageRepresentant getImagerepresentant(String ref, int offsetX,
			int offsetY) {
		// if we've already got the imagerepresentant in the cache
		// then just return the existing version
		if (imagerepresentants.get(ref) != null) {
			return (ImageRepresentant) imagerepresentants.get(ref);
		}

		// otherwise, go away and grab the imagerepresentant from the resource
		// loader
		BufferedImage sourceImage = null;

		try {
			// The ClassLoader.getResource() ensures we get the
			// imagerepresentant
			// from the appropriate place, this helps with deploying the game
			// with things like webstart. You could equally do a file look
			// up here.
			URL url = this.getClass().getResource(
					imagedir + ref);

			if (url == null) {
				fail("Can't find ref: " + imagedir + ref);
			}

			// use ImageIO to read the image in
			sourceImage = ImageIO.read(url);
		} catch (IOException e) {
			fail("Failed to load: " + ref);
		}

		// create an accelerated image of the right size to store our
		// imagerepresentant in
		GraphicsConfiguration gc = GraphicsEnvironment
				.getLocalGraphicsEnvironment().getDefaultScreenDevice()
				.getDefaultConfiguration();
		BufferedImage image = gc.createCompatibleImage(sourceImage.getWidth(),
				sourceImage.getHeight(), Transparency.BITMASK);

		// draw our source image into the accelerated image
		image.getGraphics().drawImage(sourceImage, 0, 0, null);

		// create a imagerepresentant, add it the cache then return it
		ImageRepresentant imagerepresentant = new ImageRepresentant(image,
				offsetX, offsetY);
		imagerepresentants.put(ref, imagerepresentant);

		return imagerepresentant;
	}

	/**
	 * Utility method to handle resource loading failure
	 * 
	 * @param message
	 *            The message to display on failure
	 */
	private void fail(String message) {
		// we're pretty dramatic here, if a resource isn't available
		// we dump the message and exit the game
		System.err.println(message);
		System.exit(0);
	}

	public AnimationRepresentant getAnimationrepresentant(String ref,
			long duration, boolean repeat) {
		// if we've already got the imagerepresentant in the cache
		// then just return the existing version
		if (imagerepresentants.get(ref) != null) {
			return (AnimationRepresentant) imagerepresentants.get(ref);
		}

		// otherwise, go away and grab the imagerepresentant from the resource
		// loader
		BufferedImage sourceImage = null;

		try {
			// The ClassLoader.getResource() ensures we get the
			// imagerepresentant
			// from the appropriate place, this helps with deploying the game
			// with things like webstart. You could equally do a file look
			// up here.
			URL url = this.getClass().getResource(
					imagedir + ref);

			if (url == null) {
				fail("Can't find ref: " + imagedir + ref);
			}

			// use ImageIO to read the image in
			sourceImage = ImageIO.read(url);
		} catch (IOException e) {
			fail("Failed to load: " + ref);
		}

		// create an accelerated image of the right size to store our
		// imagerepresentant in
		// GraphicsConfiguration gc =
		// GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
		// BufferedImage image =
		// gc.createCompatibleImage(sourceImage.getWidth(),sourceImage.getHeight(),Transparency.BITMASK);
		//		
		// // draw our source image into the accelerated image
		// image.getGraphics().drawImage(sourceImage,0,0,null);

		// create a imagerepresentant, add it the cache then return it
		AnimationRepresentant imagerepresentant = new AnimationRepresentant(
				sourceImage, duration, repeat);
		imagerepresentants.put(ref, imagerepresentant);

		return imagerepresentant;
	}
}